package com.example.InetAddressDemos.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author Jason
 * @PACKAGE_NAME com.example.InetAddressDemos.tcp
 * @Description 接收端
 * @date 2023-12-18 21:07
 */
public class TcpServer {
    public static void main(String[] args) throws IOException {
        //Tcp协议 发送数据

        //1.创建对象ServerSocket
        ServerSocket ss = new ServerSocket(10001);

        //2.监听客户端链接
        Socket socket = ss.accept();

        //3.从连接通道中获取输入流读取数据
        InputStream is = socket.getInputStream();
        //字节流转换为字符流，不然会出现中文乱码
        InputStreamReader isr = new InputStreamReader(is);
        //缓冲流，用于提高效率
        BufferedReader br = new BufferedReader(isr);
        int b;
        while ((b = br.read()) != -1) {
            System.out.print((char) b);
        }

        //4.释放资源
        socket.close();
        ss.close();

    }
}
